package com.qsl.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qsl.model.system.SysRole;
import com.qsl.model.system.SysUserRole;
import com.qsl.model.vo.AssginRoleVo;
import com.qsl.model.vo.SysRoleQueryVo;
import com.qsl.system.mapper.SysRoleMapper;
import com.qsl.system.mapper.SysUserRoleMapper;
import com.qsl.system.service.SysRolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service //bean
public class SysRolServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRolService {

    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    /**
     * 条件分页查询
     *
     * @param pageParam
     * @param sysRoleQueryVo
     * @return
     */
    @Override
    public IPage<SysRole> selectPage(Page<SysRole> pageParam, SysRoleQueryVo sysRoleQueryVo) {
        IPage<SysRole> pageMode = sysRoleMapper.selectPage(pageParam, sysRoleQueryVo);
        return pageMode;
    }

    /**
     * 角色分配
     *
     * @param userId
     * @return
     */
    @Override
    public Map<String, Object> getRolesByUserId(String userId) {
        // 获取所有角色
        List<SysRole> roles = sysRoleMapper.selectList(null);
        // 根据用户id查询
        QueryWrapper<SysUserRole> qw = new QueryWrapper<>();
        qw.eq("user_id", userId);
        // 获取用户已分配的角色
        List<SysUserRole> sysUserRoles = sysUserRoleMapper.selectList(qw);

        // 获取用户已分配的角色id
        List<String> userRoleIds = new ArrayList<>();
        for (SysUserRole userRole : sysUserRoles) {
            userRoleIds.add(userRole.getRoleId());
        }

        // 创建返回的map
        HashMap<String, Object> returnMap = new HashMap<>();
        returnMap.put("allRoles", roles); // 所有角色
        returnMap.put("userRoleIds", userRoleIds); // 用户分配角色id集合

        return returnMap;
    }

    /**
     * 根据用户分配角色 ==> userId,roleId
     *
     * @param assginRoleVo
     */
    @Override
    public void allotAssign(AssginRoleVo assginRoleVo) {
        System.out.println("==============》@@@" + 11111111);
        // 根据用户id删除原来分配的角色
        QueryWrapper<SysUserRole> qw = new QueryWrapper<>();
        qw.eq("user_id", assginRoleVo.getUserId());
        sysUserRoleMapper.delete(qw);

        // 获取所有角色id
        List<String> roleIdList = assginRoleVo.getRoleIdList();
        for (String roleId : roleIdList) {
            if (roleId != null) {
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setUserId(assginRoleVo.getUserId());
                sysUserRole.setRoleId(roleId);
                // 新增
                sysUserRoleMapper.insert(sysUserRole);
            }
        }
    }


}
